package com.cy.sys.service.impl;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.common.exception.ServiceException;
import com.cy.common.pojo.SysPage;
import com.cy.sys.dao.SysLogDao;
import com.cy.sys.pojo.SysLog;
import com.cy.sys.service.SysLogService;
/**
 * 此类实现了日志接口的所有方法,定义日志处理业务的逻辑
 * @author private
 *
 */
@Service
public class SysLogServiceImpl implements SysLogService{
	@Autowired
	private SysLogDao sysLogDao;
	/** 查询分页信息及数据 */
	@RequiresPermissions("sys:log:view")
	public SysPage<SysLog> findPageObjects(String username,Integer pageCurrent,Integer pageSize){
		//1.进行参数的校验
		if(pageCurrent==null || pageCurrent<1)
			throw new IllegalArgumentException("页面信息有误!");
		//2.定义并计算分页信息
		if(pageSize==null||pageSize<1) {
			throw new IllegalArgumentException("分页信息非法!");
		}
		int startIndex = (pageCurrent-1)*pageSize;
		int rowCount = sysLogDao.selectRowsCount(username);
		//3.判断是否查询到指定数据
		if(rowCount<1) throw new IllegalArgumentException("此用户没有日志信息!");
		//4.进行分页数据的查询
		List<SysLog> records = sysLogDao.selectPageObjects(username, startIndex, pageSize);
		//5.校验查询的结果信息
		if(records==null) throw new ServiceException("查询数据失败!");
		//6.进行分页数据的封装
		SysPage<SysLog> page = new SysPage<>(pageCurrent, rowCount, pageSize, records);
		//5.返回结果给调用方
		return page;
	}
	
	@RequiresPermissions("sys:log:delete")
	@Transactional(rollbackFor = Exception.class)
	/** 删除由用户端传过来的id数组对应的日志信息 */
	public int deleteObjects(Integer... ids) {
		//1.进行参数的校验
		if(ids==null||ids.length<1) throw new IllegalArgumentException("请先选择要删除的日志!");
		//2.进行删除日志操作
		int rows = sysLogDao.deleteObjectsById(ids);
		return rows;
	}

	@Override
	@Async //启用异步处理
	public void addLog(SysLog sysLog) {
		if(sysLog!=null)
			sysLogDao.insertLog(sysLog);
		
	}
}
